package dao;

import entity.Product;
import entity.Purchase;
import entity.PurchaseDetails;
import entity.SaleDetails;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PurchaseDetailsDao extends BaseDao {
    //列表所有供货单明细信息
    public List getAllpurchaseDetails() throws Exception {
        List purchaseDetailList = new ArrayList();
        String sql = "select purchaseDetailsId,purchaseId,productId,purchaseNumber" +
                " from purchaseDetails";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String purchaseDetailsId = resultSet.getString("purchaseDetailsId");
            String purchaseId = resultSet.getString("purchaseId");
            String productId = resultSet.getString("productId");
            int purchaseNumber = resultSet.getInt("purchaseNumber");
            PurchaseDetails purchaseDetails = new PurchaseDetails(purchaseDetailsId,purchaseId,productId,purchaseNumber);
            purchaseDetailList.add(purchaseDetails);
        }
        return purchaseDetailList;
    }

    //录入供货单明细信息
    public boolean addPurchaseDetail(PurchaseDetails purchaseDetails) throws Exception {
        String sql = "insert into purchaseDetails(purchaseDetailsId,purchaseId,productId,purchaseNumber)" +
                " VALUES(?,?,?,?)";
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, purchaseDetails.getPurchaseDetailsId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, purchaseDetails.getPurchaseId());
        //设置第二个？占位符的值
        preparedStatement.setString(3, purchaseDetails.getProductId());
        //设置第三个？占位符的值
        preparedStatement.setInt(4, purchaseDetails.getPurchaseNumber());
        //设置第四个？占位符的值

        int num = preparedStatement.executeUpdate();//执行sql语句
        if (num==1)
        {
            return true;
        }else {
            return false;
        }
    }

    //列表指定订单下的订单明细信息
    public List getAppointPurchaseDeatils(String purchaseIdTemp) throws Exception {
        List purchaseDetailsList = new ArrayList();
        String sql = "select purchaseDetailsId,purchaseId,productId,purchaseNumber" +
                " from purchasedetails where purchaseId='" + purchaseIdTemp + "'";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String purchaseDetailsId = resultSet.getString("purchaseDetailsId");
            String purchaseId = resultSet.getString("purchaseId");
            String productId = resultSet.getString("productId");
            int purchaseNumber = resultSet.getInt("purchaseNumber");

            PurchaseDetails purchaseDetails = new PurchaseDetails(purchaseDetailsId,purchaseId,productId,purchaseNumber);
            purchaseDetailsList.add(purchaseDetails);
        }
        return purchaseDetailsList;
    }

    //删除供货单详情信息
    public String deletePurchaseDetails(PurchaseDetails purchaseDetails) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from purchaseDetails where purchaseDetailsId=?";//定义sql语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,purchaseDetails.getPurchaseDetailsId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }



    public static void main(String[] args) throws Exception {
        PurchaseDetailsDao purchaseDetailsDao = new PurchaseDetailsDao();
        List purchaseDetailsList = purchaseDetailsDao.getAppointPurchaseDeatils("PN2306183670191");
        for (int i = 0; i < purchaseDetailsList.size(); i++) {
            PurchaseDetails purchaseDetails = (PurchaseDetails) purchaseDetailsList.get(i);
            purchaseDetailsDao.deletePurchaseDetails(purchaseDetails);
        }
        PurchaseDao purchaseDao = new PurchaseDao();
        Purchase purchase = purchaseDao.getAppointPurchase2("PN2306183670191");
        purchaseDao.deletePurchase(purchase);
    }
}
